/**
 * Kalaisoo
 *
 * A lightweight no-framework framework written in PHP
 *
 * @package Kalaisoo
 * @author $Author: info@sah-company.com $
 */

/**
 * The delay for out keyup event in q (global search) in miliseconds
 *
 * @var int
 */
var qDelay = 333;

/**
 * The onkey timer of the global search box
 *
 * @var mixed
 */
var qTimer = null;

/**
 * The delay for the system heartbeat
 *
 * @var int
 */
var bDelay = 3333;

/**
 * This is the previously opened drawer (of the toolbar)
 *
 * @var mixed
 */
var previousDrawer = null;

/**
 * document.ready jquery stuff
 */
$(document).ready(function() {
	
	/**
	 * Every bDelay microseconds the heartbeat URL is called and new items are displayed or
	 * the count of new items is displayed, depending on which page the client currently is.
	 */
	heartbeat = setInterval(function() {
		e = $('#heartbeat');
		if (e.length == 0) {
			clearInterval(heartbeat);
			return;
		}
		//$("#toolbox").addClass('running');
		type = 'items';
		response = $("#response");
		if (response.length == 0) {
			type = 'counter';
		}
		$.get(e.attr('data-target')+type,
			function(data) {
				if (response.length == 0) {
					$('#heartbeat').empty();
					$('#heartbeat').append(data);
					if (data) {
						$("#heartbeat").show('slow');
					} else {
						$("#heartbeat").hide('slow');
					}
				} else {
					$('#response').prepend(data);
					$("#heartbeat").hide('slow');
				}
				//$("#toolbox").removeClass('running');
			},
			'html'
		);
	}
	, bDelay);
	
	/**
	 * search box fires requests at find/tags/ whenever a keyup event occurs
	 * but only if a element with id #main is found and the qTimer is not still running
	 * and forcing a delay to the server requests.
	 * Repsonse of find/tags is output into the #main element.
	 *
	 */
	/*
	$('#q').keyup(function() {
		e = $('#searchresults');
		if (e.length == 0) {
			return;
		}
		if (qTimer) {
			window.clearTimeout(qTimer);
		}
		qTimer = window.setTimeout(function() {
			qTimer = null;
			//$("#toolbox").addClass('running');
		  	$.post($("#search").attr("action"), $("#search").serialize(),
				function(data) {
					$("#searchresults").empty();
					$("#searchresults").prepend(data);
					//$("#toolbox").removeClass('running');
				},
				'html'
			);
		}, qDelay);
	});
	*/
	
	/**
	 * open main navigation when clicking on avatar
	 */
	$("#usr").click(function() {
		$("div.workbench").hide();
		$("nav.main").toggle('fast');
		return false;
	});
	
	/**
	 * open main navigation when clicking on avatar
	 */
	$("#wrench").click(function() {
		$("nav.main").hide();
		$("div.workbench").toggle('fast');
		return false;
	});
	
	/**
	 * loadOnScroll
	 *
	 * This is called whenever you scroll to the end of a page. It checks for an element with
	 * id loadOnScroll. If there is one it's url is send as a GET request and the resulting
	 * data is appended to an element with id result.
	 */
	function loadOnScroll()
	{
		e = $('#loadOnScroll');
		if (e.length == 0) {
			return;
		}
		target = e.attr('data-target');
		$.get(e.attr('href'),
			function(data) {
				$('#loadOnScroll').remove();
				$('#'+target).append(data);
			},
			'html'
		);
	}

	/**
	 * Check if user scrolls beyond page end and calls loadPosts() if so
	 *
	 * @link http://www.webresourcesdepot.com/load-content-while-scrolling-with-jquery/
	 */
	$(window).scroll(function () {
		if ($(window).scrollTop() == $(document).height() - $(window).height()) {
			loadOnScroll();
		}
	})
	
});